<?php
/**
 * 首页
 * @version 1.0.0 (Thu Feb 23 13:49:18 GMT 2012)
 * @author ZhangHao
 */

class Api extends CI_Controller {
	private $_data;

    public function __construct() {
		parent::__construct();
		$this->load->model('base_mdl', 'base');
    }

	public function index() {

		$page = $this->input->get('page') > 1 ? $this->input->get('page') : '1';
		$this->_data['type'] = $type = $this->input->get('type');
		$limit = 30;
		$offset = ($page - 1) * $limit;
		$where  = array('state'=>1);
		$desc = 'ctime DESC';

		if($type) {
			if(intval($type) > 0) {
				$where['type'] = $type; 
			} else if($type == 'hot') {
				$desc = 'hits DESC, ctime DESC';
			} else if($type == 'top') {
				$where['top'] = 1; 
				$desc = 'toptime DESC';
			}
		}

		$total_num = $this->base->get_data('albums', $where)->num_rows();

		$lists = $this->base->get_data('albums', $where, 'title, id, cover', $limit, $offset, $desc)->result_array();

		$html = '';
		foreach ($lists as $row) {
			$html .= "<span data-id='".$row['id']."' data-title='".$row['title']."' class='album' onClick=\"goDetail('".$row['id']."', '".$row['title']."')\"><img src='".get_thumb($row['cover'])."'/></span>";
		}

		$data = array('page'=>$page, 'html'=>$html);
		output(1, $data);
	}

	function detail() {
		$aid = intval($_GET['id']);

		$album = $this->base->get_data('albums', array('id'=>$aid), 'title')->row_array();
		$lists = $this->base->get_data('images', array('aid'=>$aid), 'local_url')->result_array();
		$this->db->query('UPDATE mt_albums SET hits=hits+1 WHERE id='.$aid);
		$imgUrl = array();

		foreach($lists as $row) {
			$imgUrl[] = get_thumb($row['local_url'], false).'-image1';
		}

		output(1, array('title'=>$album['title'], 'imgUrl'=>$imgUrl));

	}

}